package com.easylinkin.linkappapi.meterbilling.controller;

import com.easylinkin.linkappapi.meterbilling.entity.RechargeRecord;
import com.easylinkin.linkappapi.meterbilling.entity.ext.WechatRechargeInfo;
import com.easylinkin.linkappapi.meterbilling.service.RechargeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * class info :房间充值
 *
 * @author liuqihang
 * @date 2021/7/15 9:54
 */
@RestController
@RequestMapping("/recharge")
public class RechargeController {

    @Resource
    private RechargeService rechargeService;

    @PostMapping("addBalance")
    public RestMessage addBalance(@RequestBody RechargeRecord rechargeRecord) {
        Assert.notNull(rechargeRecord, "rechargeRecord 不能为空");
        Assert.notNull(rechargeRecord.getContractId(), "住户合同ID 不能为空");
        Assert.notNull(rechargeRecord.getRechargeType(), "充值类型 不能为空");
        Assert.notNull(rechargeRecord.getPrice(), "充值金额 不能为空");
        Assert.notNull(rechargeRecord.getPayTime(), "支付时间 不能为空");
        String result = rechargeService.addBalance(rechargeRecord);
        if(result != null){
            return RestBuilders.failureBuilder().message(result).build();
        }
        return RestBuilders.successBuilder().message("success").build();
    }

    @PostMapping("thirdWXPublic")
    public RestMessage addBalance(@RequestBody WechatRechargeInfo wechatRechargeInfo) {
        Assert.notNull(wechatRechargeInfo, "wechatRechargeInfo 不能为空");
        Assert.notNull(wechatRechargeInfo.getRoomId(), "房间ID不能为空");
        Assert.notNull(wechatRechargeInfo.getRechargeType(), "充值类型 不能为空");
        Assert.notNull(wechatRechargeInfo.getMoney(), "充值金额 不能为空");
        return rechargeService.createWXOrder(wechatRechargeInfo);
    }

    /**
     * LinkThings支付回调：水电表管理
     * @param request
     * @param response
     */
    @PostMapping("/thirdAsynNotify")
    public void asynThirdNotify(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        rechargeService.asynThirdNotify(parameterMap, response);
    }

    @GetMapping("/askOrderStatus/{orderNo}")
    public RestMessage askOrderStatus(@PathVariable String orderNo) {
        Assert.notNull(orderNo, "orderNo 不能为空");
        return rechargeService.askOrderStatus(orderNo);
    }




}
